#pragma comment(lib, "ws2_32.lib")
#if 0
#include <sys/types.h>

#include <event2/event-config.h>

#include <sys/stat.h>
#ifndef WIN32
#include <sys/queue.h>
#include <unistd.h>
#endif
#include <time.h>
#ifdef _EVENT_HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/util.h>

#ifdef WIN32
#include <winsock2.h>
#endif

struct timeval lasttime;

int event_is_persistent;

static void timeout_cb(evutil_socket_t fd, short event, void *arg)
{
	struct timeval newtime, difference;
	struct event *timeout = arg;
	double elapsed;

	evutil_gettimeofday(&newtime, NULL);
	evutil_timersub(&newtime, &lasttime, &difference);
	elapsed = difference.tv_sec +
			  (difference.tv_usec / 1.0e6);

	printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
		   (int)newtime.tv_sec, elapsed);
	lasttime = newtime;

	if (!event_is_persistent)
	{
		struct timeval tv;
		evutil_timerclear(&tv);
		tv.tv_sec = 2;
		event_add(timeout, &tv);
	}
}

int main(int argc, char **argv)
{
	struct event timeout;
	struct timeval tv;
	struct event_base *base;
	int flags;

#ifdef WIN32
	WORD wVersionRequested;
	WSADATA wsaData;

	wVersionRequested = MAKEWORD(2, 2);

	(void)WSAStartup(wVersionRequested, &wsaData);
#endif

	if (argc == 2 && !strcmp(argv[1], "-p"))
	{
		event_is_persistent = 1;
		flags = EV_PERSIST;
	}
	else
	{
		event_is_persistent = 0;
		flags = 0;
	}

	/* Initalize the event library */
	base = event_base_new();

	/* Initalize one event */
	event_assign(&timeout, base, -1, flags, timeout_cb, (void *)&timeout);

	evutil_timerclear(&tv);
	tv.tv_sec = 2;
	event_add(&timeout, &tv);

	evutil_gettimeofday(&lasttime, NULL);

	event_base_dispatch(base);

	return (0);
}
#endif

#if 0
#include <stdio.h>
#include <event.h>
#include <time.h>

struct event ev;
struct timeval tv;

void time_fn(int fd, short event, void *arg)
{
	printf("timer function\n");
	event_add(&ev, &tv); // add into struct event instance
}

int main(int argc, char const *argv[])
{
#ifdef _WIN32
	// Windows must set
	// ref links:https://www.cnblogs.com/GengMingYan/p/15491640.html
	WSADATA wsa_data;
	WSAStartup(0x0201, &wsa_data);
#endif
	struct event_base *base = event_init(); // init libevent
	tv.tv_sec = 1;
	tv.tv_usec = 0; // 1 second interval then call time_fn

	event_set(&ev, -1, 0, time_fn, NULL);

	event_add(&ev, &tv);

	event_base_dispatch(base);

	return 0;
}
#endif


#include <stdio.h>
#include <iostream>
#include <event.h>
#include <event2/http.h>
#include <event2/http_struct.h>
#include <event2/http_compat.h>
#include <event2/event-config.h>
using namespace std;

int main(void) {
    WSADATA wsa_data;
    WSAStartup(0x0201, &wsa_data);
    event_init();
    char *httpd_option_listen = "0.0.0.0";
    int httpd_option_port = 6969;
    struct evhttp *evHttp = evhttp_start(httpd_option_listen, httpd_option_port);
    if (evHttp == NULL) {
        cout << "错误!";
        getchar();
        return 0;
    }
    evhttp_set_timeout(evHttp, 5);
    evhttp_set_cb(evHttp, "/get", [](struct evhttp_request * req, void * data) {
         size_t len = evbuffer_get_length(req->input_buffer);
        cout << "req len:" << len;
        evbuffer *respBuff = evbuffer_new();
        evbuffer_add_printf(respBuff, "Hello ? World!");
        evhttp_send_reply(req, HTTP_OK, "Client", respBuff);
        evbuffer_free(respBuff);
    }, NULL);

    event_dispatch();
    evhttp_free(evHttp);
    return 0;
}